<?php

class AdvanceprofileController extends AdminController {    
	
    public function actionIndex($id) {
    	$model = $this->loadModelByProfileId();
    	$modelProfile = Profile::model()->findByPk($_GET['id']);
    	
		if(count($model) == 0){ //There is no translated model so, create new profile for translate
			$model = $this->createAdvanceProfile($id);
			$msg = t('MSG_INFO_INSERT_SUCCESS', 'messages' , array('{0}' => t('LBL_ADVANCE_PROFILE','advanceProfile')));
		}else{//The translated model is existing so create a new profile for translate
			$model = $model[0];
			$model = $this->updateAdvanceProfile($model);
			$msg = t('MSG_INFO_UPDATE_SUCCESS', 'messages' , array('{0}' => t('LBL_ADVANCE_PROFILE','advanceProfile')));
		}
		if (isset($_POST['AdvanceProfile'])){
			$this->showMessage(MsgType::SUCCESS,$msg);
			if (isset($_POST["cmsTask"])) {
				$cmsTask = $_POST["cmsTask"];
				switch ($cmsTask) {
					case ActionType::SAVE: {
						$url = url($this->id . '/index', array('id'=>$id));
						$this->redirect($url);
						break;
					}
					case ActionType::SAVE_CLOSE: {
						$this->redirect(url('/profile/'));
						break;
					}
					case ActionType::SUBMIT: {
						$this->redirect(url('/translate/index/', array('id'=>$id)));
						break;
					}
				}
			}
			app()->end();
		}else{
			$this->render('form', array('model' => $model, 'modelProfile' => $modelProfile));
		}
    }
    
    function actions() {
        return array(
          'create' => array(
            'class' => 'lib.components.actions.CreateAction',
            'modelClass' => 'AdvanceProfile',
            'messages' => array('success' => t('MSG_INFO_INSERT_SUCCESS', 'messages' , array('{0}' => t('LBL_ADVANCE_PROFILE','advanceProfile'))))
          ),
          'update' => array(
            'class' => 'lib.components.actions.UpdateAction',
            'modelClass' => 'AdvanceProfile',
            'messages' => array('success' => t('MSG_INFO_UPDATE_SUCCESS', 'messages', array('{0}' => t('LBL_ADVANCE_PROFILE','advanceProfile'))))
          ),
          'delete' => array(
            'class' => 'lib.components.actions.DeleteAction',            
            'ajaxView' => "//share/_delete",
            'messages' => array('success' => t('MSG_INFO_DELETE_SUCCESS', 'messages' , array('{0}' =>t('LBL_ADVANCE_PROFILE','advanceProfile'))))
          ),
          'error' => array(
            'class' => 'lib.components.actions.ErrorAction',
          ),
        );
    }
	
    public function actionGetAllOperation(){
        $allcontrollers = AdvanceProfile::model()->getAllController();
        $actions = array();
        foreach ($allcontrollers as $key => $controller) {
             $class = $controller."Controller";
                
                $hascombine =  method_exists ( $class , 'actions' );
                array_push($actions,$controller.'.'.'*');
                if($hascombine == 1){
                    $combinedActions = call_user_func(array($class, 'actions'));
                    foreach ($combinedActions as $key => $value){
                        array_push($actions,$controller.".".ucfirst($key));
                    }
                }

                $allmethods =get_class_methods($class);
                foreach ($allmethods as $key => $value){
                    if(strlen(strstr($value,'action'))>0){
                        if($value != 'actions'){
                            $value = str_replace('action', '', $value);
                            array_push($actions,$controller.".".ucfirst($value));
                        }
                    }
                }
        }
        echo CJSON::encode($actions);
    }
    
    public function loadModelByProfileId() {
        $model = null;
        if (isset($_GET['id']))
        {
            $ids = explode(',', $_GET['id']);
            if (isset($ids)) {
                $criteria = new CDbCriteria;
                $criteria->addInCondition('profile_id', $ids);
                $criteria->with = array (
					'profile' 
				);
                $criteria->with = array (
                		'hiringCompany'
                );
                $model = AdvanceProfile::model()->findAll($criteria);
            }         
        }
        return $model;
        
    }

    public function editDialogButtonHtml($advanceProfileModel, $dialogTitle, $dialogWidth, $dialogHeight) {
	$updateUrl = url(app()->controller->id . "/update/" . $advanceProfileModel->id);

	return Html::buttonInGrid('linkEdit', $updateUrl, 'Edit', $dialogTitle, 'icon edit update-dialog-open-link', $dialogWidth, $dialogHeight);
    }
    
    private function createAdvanceProfile($id){
    	$model = new AdvanceProfile('create');
    	//Set value to missing field and reset values of translated fields.
    	if (method_exists($model, 'unsetAttributes')) {
    		$model->unsetAttributes();
    	}
    	$model->hiringCompany = new HiringCompany('create');
    	$model->introducingCompany = new IntroducingCompany('create');
    	$model->hiringCompany->union = new Union('create');
    	$model->profile_id = $id;
    	if (isset($_POST['AdvanceProfile'])) {
    		$model->attributes = $_POST['AdvanceProfile'];
    		$model->profile_id = $id;
    		if ($model->validate()){
    			$model->save(true);
    		}
    	}
    	return $model;
    }
    
    private function updateAdvanceProfile($model){
    	if (isset($_POST['AdvanceProfile'])) {
    		$oldContract = $model->url_contract;
    		$oldVisa = $model->url_visa;
    		$oldPhoto = $model->url_id_card_photo;
    		$model->attributes = $_POST['AdvanceProfile']; 
    		
    		if ($model->validate()){
    			$model->save(true);
    		}
    	}
    	return $model;
    }
}

?>